﻿package com.sgcc.nfc.lock.presentation.warnings

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.sgcc.nfc.lock.R
import com.sgcc.nfc.lock.databinding.ItemWarningDetailBinding
import com.sgcc.nfc.lock.domain.model.Warning
import com.sgcc.nfc.lock.domain.model.WarningStatus
import com.sgcc.nfc.lock.domain.model.WarningType

class WarningsAdapter(
    private val onItemClick: (Warning) -> Unit
) : ListAdapter<Warning, WarningsAdapter.WarningViewHolder>(DiffCallback) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WarningViewHolder {
        val binding = ItemWarningDetailBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return WarningViewHolder(binding)
    }

    override fun onBindViewHolder(holder: WarningViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class WarningViewHolder(
        private val binding: ItemWarningDetailBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: Warning) {
            val ctx = binding.root.context
            binding.warningTypeText.text = when (item.warningType) {
                WarningType.FORCED_OPEN -> ctx.getString(R.string.warning_type_forced_open)
                WarningType.TIMEOUT -> ctx.getString(R.string.warning_type_timeout)
                WarningType.BATTERY_LOW -> ctx.getString(R.string.warning_type_battery_low)
                WarningType.MALFUNCTION -> ctx.getString(R.string.warning_type_malfunction)
                WarningType.UNKNOWN -> ctx.getString(R.string.warning_type_unknown)
            }
            binding.warningContentText.text = item.warningContent ?: "--"
            binding.warningRoomText.text = item.roomName ?: "-"
            binding.warningStatusText.apply {
                text = when (item.status) {
                    WarningStatus.PENDING -> ctx.getString(R.string.warning_status_pending_label)
                    WarningStatus.HANDLED -> ctx.getString(R.string.warning_status_handled_label)
                    WarningStatus.CLOSED -> ctx.getString(R.string.warning_status_closed_label)
                    WarningStatus.UNKNOWN -> ctx.getString(R.string.warning_status_unknown_label)
                }
                setTextColor(
                    ContextCompat.getColor(
                        ctx,
                        when (item.status) {
                            WarningStatus.PENDING -> R.color.color_status_error
                            WarningStatus.HANDLED -> R.color.color_status_success
                            WarningStatus.CLOSED -> R.color.color_status_success
                            WarningStatus.UNKNOWN -> android.R.color.darker_gray
                        }
                    )
                )
            }
            binding.warningTimeText.text = item.createTime ?: "--"
            binding.root.setOnClickListener { onItemClick(item) }
        }
    }

    companion object DiffCallback : DiffUtil.ItemCallback<Warning>() {
        override fun areItemsTheSame(oldItem: Warning, newItem: Warning): Boolean = oldItem.warningId == newItem.warningId
        override fun areContentsTheSame(oldItem: Warning, newItem: Warning): Boolean = oldItem == newItem
    }
}


